﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notepad
{
    public partial class FontForm : Form
    {
        public FontForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FontForm_Load(object sender, EventArgs e)
        {
            //获取字体
            GetFontFamily();
            //获取字形
            GetFontStylte();
            //获取大小
            GetFontSize();

            //默认选中
            this.lbFontFamily.SelectedIndex = 0;
            this.lbFontStyle.SelectedIndex = 0;
            this.lbFontSize.SelectedIndex = 3;
        }
        /// <summary>
        /// 字体类型
        /// </summary>
        private void GetFontFamily()
        {
            InstalledFontCollection ifc = new InstalledFontCollection();
            foreach (FontFamily ff in ifc.Families)//循环读取系统自带的字体类型
            {
                this.lbFontFamily.Items.Add(ff.Name);
            }
        }
        /// <summary>
        /// 字形
        /// </summary>
        private void GetFontStylte()
        {
            //通过Enum读取枚举类型FontStyle 通过typeof获取FontStyle实例
            foreach (FontStyle fs in Enum.GetValues(typeof(FontStyle)))
            {
                this.lbFontStyle.Items.Add(fs);
            }
        }
        /// <summary>
        /// 字体大小
        /// </summary>
        private void GetFontSize()
        {
            for (int i = 8; i <= 72; i += 2)
            {
                this.lbFontSize.Items.Add(i);
            }
        }

        //定义一个委托事件 event事件 Action委托实例 所有窗体都能用
        public event Action<Font> FontSave;//默认为null

        /// <summary>
        /// 把选中的字体信息回传到NotepadFrom窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            //组织Font类型返回一个Font           
            string fontFamliy = this.lbFontFamily.Text;
            //float.Parse将字符串转换成浮点型
            float size = float.Parse(this.lbFontSize.Text);
            //(FontStyle)强制转换成枚举类型
            FontStyle fs = (FontStyle)Enum.Parse(typeof(FontStyle),this.lbFontStyle.Text);
            //组织并创建一个Font类型  
            Font font = new Font(fontFamliy,size,fs);

            //字体信息保存到文本 如果存在会覆盖
            File.WriteAllText(Config.filePath, $"{fontFamliy},{size},{fs}");

            FontSave(font);

            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
